package com.jxmcloud.business.cut.service.impl.database;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jxmcloud.business.cut.mapper.database.IInviteSuccessDBMapper;
import com.jxmcloud.business.cut.model.database.DBUserInviteSuccess;
import com.jxmcloud.business.cut.model.query.InviteSuccessQuery;
import com.jxmcloud.business.cut.service.database.IUserInviteSuccessDBService;
import com.jxmcloud.business.cut.vo.InviteSuccessVo;
import com.xlcloud.platform.util.PublicUtil;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service("inviteSuccessDBServiceImplNew")
public class UserInviteSuccessDBServiceImpl extends ServiceImpl<IInviteSuccessDBMapper, DBUserInviteSuccess> implements IUserInviteSuccessDBService {
    @Override
    public boolean insertIntoData(DBUserInviteSuccess info) {
        return this.baseMapper.insert(info) == 1;
    }

    @Override
    public List<DBUserInviteSuccess> selectAll(Integer uid, Integer activityId) {
        QueryWrapper<DBUserInviteSuccess> wrapper = new QueryWrapper<>();
        wrapper.eq("uid", uid);
        wrapper.eq("activity_id", activityId);
        return this.baseMapper.selectList(wrapper);
    }

    @Override
    public boolean deleteData(Integer uid) {
        QueryWrapper<DBUserInviteSuccess> wrapper = new QueryWrapper<>();
        wrapper.eq("uid", uid);
        return this.baseMapper.delete(wrapper) >= 1;
    }

    @Override
    public boolean updateData(DBUserInviteSuccess info) {
        return this.baseMapper.updateById(info) == 1;
    }

    @Override
    public DBUserInviteSuccess selectHelpInfo(Integer uid, Integer helperXlId, Integer activityId) {
        QueryWrapper<DBUserInviteSuccess> wrapper = new QueryWrapper<>();
        wrapper.eq("uid", uid);
        wrapper.eq("invited_xl_id", helperXlId);
        wrapper.eq("activity_id", activityId);
        List<DBUserInviteSuccess> dbInviteSuccesses = this.baseMapper.selectList(wrapper);
        if(dbInviteSuccesses.isEmpty()) {
            return null;
        }
        return dbInviteSuccesses.get(0);
    }

    @Override
    public DBUserInviteSuccess selectCanGainAwardInfo(Integer uid, Integer activityId) {
        QueryWrapper<DBUserInviteSuccess> wrapper = new QueryWrapper<>();
        wrapper.eq("uid", uid);
        wrapper.eq("activity_id", activityId);
        wrapper.eq("is_gain_award", 0);
        List<DBUserInviteSuccess> list = this.baseMapper.selectList(wrapper);
        if(list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public List<DBUserInviteSuccess> selectCanGainNewerAwardInfoAll(Integer uid, Integer activityId) {
        QueryWrapper<DBUserInviteSuccess> wrapper = new QueryWrapper<>();
        wrapper.eq("uid", uid);
        wrapper.eq("activity_id", activityId);
        wrapper.eq("user_type", 1);
        wrapper.eq("is_gain_award", 0);
        return this.baseMapper.selectList(wrapper);
    }

    @Override
    public List<DBUserInviteSuccess> selectInviteNewersList(Integer uid, Integer activityId) {
        QueryWrapper<DBUserInviteSuccess> wrapper = new QueryWrapper<>();
        wrapper.eq("uid", uid);
        wrapper.eq("activity_id", activityId);
        wrapper.eq("user_type", 1);
        return this.baseMapper.selectList(wrapper);
    }

    @Override
    public int selectInviteNewersCount(Integer uid, Integer activityId) {
        QueryWrapper<DBUserInviteSuccess> wrapper = new QueryWrapper<>();
        wrapper.eq("uid", uid);
        wrapper.eq("activity_id", activityId);
        wrapper.eq("user_type", 1);
        return this.baseMapper.selectCount(wrapper).intValue();
    }

    @Override
    public boolean updateGainAward(Integer id) {
        // 更新 is_gain_award = 1
        DBUserInviteSuccess dbInviteSuccess = this.baseMapper.selectById(id);
        dbInviteSuccess.setIsGainAward(1);
        return this.baseMapper.updateById(dbInviteSuccess) == 1;
    }

    @Override
    public IPage<InviteSuccessVo> getInviteSuccessPage(InviteSuccessQuery query) {
        return this.baseMapper.selectInviteSuccessPage(query);
    }

    @Override
    public boolean checkExitByInvitedAccount(String account) {
        PublicUtil.ifNullAndThrow(account,"入参不能为空");
        return this.lambdaQuery().eq(DBUserInviteSuccess::getInvitedAccount,account).exists();
    }
}
